<template>
  <div style="height: 100%">
    <div class="table-box">
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="group_name" label="群组名称" align="center" />
        <el-table-column prop="weight" label="权重" align="center" />
        <el-table-column fixed="right" label="操作" width="380" align="center">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              @click="showPrivateMsg(scope.row)"
            >
              群发消息
            </el-button>
            <el-button disabled type="primary" size="small">弹窗消息</el-button>
            <el-button disabled type="danger" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog
      v-model="dialogFormVisible"
      :title="'私信给: ' + currentgroup.group_name"
      width="500"
    >
      <el-form :model="form">
        <el-form-item label="内容" :label-width="formLabelWidth">
          <el-input
            type="textarea"
            :rows="6"
            v-model="form.content"
            autocomplete="off"
          />
        </el-form-item>
        <!-- <el-form-item label="Zones" :label-width="formLabelWidth">
          <el-select v-model="form.region" placeholder="Please select a zone">
            <el-option label="Zone No.1" value="shanghai" />
            <el-option label="Zone No.2" value="beijing" />
          </el-select>
        </el-form-item> -->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="sendMsg"> 发送 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { loadGroups, sendGroupMsg } from "@/api/user";
import { nextTick, onMounted, reactive, ref } from "vue";
const formLabelWidth = "90px";

const total_count = ref(0);
const dialogFormVisible = ref(false);
const currentgroup = ref<Group>({
  group_name: "",
  weight: 0,
  group_id: ""
});

interface Group {
  group_name: string;
  weight: number;
  group_id: string;
}

const form = reactive({
  content: ""
});

const showPrivateMsg = (group: Group) => {
  currentgroup.value = group;
  nextTick(() => {
    dialogFormVisible.value = true;
  });
  console.log("showPrivateMsg", group);
  // 跳转到私信页面
  // window.open(`/private-msg?user_id=${user.ID}`, "_blank");
};

const load_usrs = () => {
  loadGroups().then(res => {
    console.log("mounted", res);
    if (res.code === 200) {
      tableData.value = res.data;
      total_count.value = res.data;
    }
  });
};

const sendMsg = () => {
  console.log("sendMsg", form);
  dialogFormVisible.value = false;
  sendGroupMsg({
    to_id: currentgroup.value.group_id,
    content: form.content,
    message_type: "text",
    image_urls: []
  }).then(res => {
    console.log("sendMsg", res);
  });
};

onMounted(() => {
  load_usrs();
});

const tableData = ref([]);
</script>

<style lang="scss" scoped>
.table-box {
  height: calc(100vh - 100px);
  overflow-y: auto;
}

.pager-box {
  border-top: solid 1px #eee;
  height: 90px;
  width: 100%;
  background-color: #fff;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px 0;
}
</style>
